package com.apa7.keepaccounts.controller;

import com.alibaba.fastjson.JSONObject;
import com.apa7.keepaccounts.dto.ReturnData;
import com.apa7.keepaccounts.service.StatisticsService;
import com.apa7.keepaccounts.util.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/statistics")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    @RequestMapping("/index")
    public ReturnData index(){
        JSONObject data= Tool.getJsonOject();
        if(!data.containsKey("key")){
            return new ReturnData("获取数据失败！");
        }
        if(!data.containsKey("date")){
            return new ReturnData("获取数据失败！");
        }

        JSONObject statisticsServiceData=statisticsService.index(data.getString("key"),data.getString("date"));
        if(statisticsServiceData==null){
            return new ReturnData(405,"请重新登录");
        }

        return new ReturnData(0,statisticsServiceData);
    }

    @RequestMapping("/chart")
    public ReturnData chart(){
        JSONObject data= Tool.getJsonOject();
        if(!data.containsKey("key")){
            return new ReturnData("获取数据失败！");
        }
        if(!data.containsKey("date")){
            return new ReturnData("获取数据失败！");
        }

        JSONObject statisticsServiceData=statisticsService.chart(data.getString("key"),data.getString("date"));
        if(statisticsServiceData==null){
            return new ReturnData("获取数据失败！");
        }

        return new ReturnData(0,statisticsServiceData);
    }
}
